﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SES.CMS.AdminCP;
using SES.CMS.BL;
using SES.CMS.DAL;
using SES.CMS.DO;

namespace SES.CMS.AdminCP
{
    public partial class Tag : System.Web.UI.Page
    {
        cmsTagDO objTag = new cmsTagDO();
        static bool insert = false;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                
                View();
            }
        }

        void View()
        {
           
                listDetail.DataSource = new cmsTagBL().SelectAll();
                listDetail.DataBind();
                pnInsert.Visible = false;
           
        }

        protected void themmoi_Click(object sender, EventArgs e)
        {
            insert = true;
            txtLink.Text = "";
            txtOrder.Text = "";
            txtTitle.Text = "";
            txtTitle.Focus();
            pnInsert.Visible = true;
        }

        
        
        protected void listDetail_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            listDetail.PageIndex = e.NewPageIndex;
            View();
        }

        protected void listDetail_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            int id = int.Parse(e.CommandArgument.ToString());
            objTag.TagID = id;
            Session["TagID"] = id;
            objTag = new cmsTagBL().Select(objTag);
            switch (e.CommandName)
                 
            {
                //case "Delete":
            		
                //    new cmsTagBL().Delete(objTag);
                //    Functions.Alert("Xóa bản tin thành công!");
                //    View();
                //    break;
                case "Select":
                    txtTitle.Text = objTag.Title;
                    txtOrder.Text = Convert.ToString(objTag.OrderID);
                    txtLink.Text = objTag.Link;
                    ddlType.SelectedValue = Convert.ToString(objTag.Type);
                    pnInsert.Visible = true;
                    break;
            }

        }
        protected void Initobject()
        {
            objTag = new cmsTagBL().Select(objTag);
            objTag.Title = txtTitle.Text;
            objTag.OrderID = int.Parse(txtOrder.Text);
            objTag.Link = txtLink.Text;
            objTag.Type = int.Parse(ddlType.SelectedValue.ToString());
        }
        protected void lbtLuu_Click(object sender, EventArgs e)
        {
            if (insert)
            {
                objTag = new cmsTagBL().Select(objTag);
                Initobject();
                new cmsTagBL().Insert(objTag);
                Functions.Alert("Thêm mới thành công!!!");
                View();
            }
            else
            {
                
                objTag = new cmsTagBL().Select(objTag);
                objTag.TagID = int.Parse(Session["TagID"].ToString());
                Initobject();
                new cmsTagBL().Update(objTag);
                Functions.Alert("Cập nhập thành công!!!");
                View();
            }
        }
        
        protected void listDetail_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            objTag.TagID = int.Parse(Session["TagID"].ToString());
            new cmsTagBL().Delete(objTag);
            Functions.Alert("Xóa bản ghi thành công!!!");
            View();
        }

        protected void lbtHuy_Click(object sender, EventArgs e)
        {
            View();
        }

        protected void dropSort_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (dropSort.SelectedValue == "0")
            {
                listDetail.DataSource = new cmsTagBL().GetByType(0, 0);
                listDetail.DataBind();
                pnInsert.Visible = false;
                View();
            }
            else if (dropSort.SelectedValue == "1")
            {
                listDetail.DataSource = new cmsTagBL().GetByType(0, 1);
                listDetail.DataBind();
            }
            else if (dropSort.SelectedValue == "2")
            {
                listDetail.DataSource = new cmsTagBL().GetByType(0, 2);
                listDetail.DataBind();
            }
        }
    }
}